import 'package:flutter/material.dart';

import 'package:get/get.dart';
import 'package:xmshop/app/modules/cart/views/cart_item_view.dart';
import 'package:xmshop/app/serives/screenAdapter.dart';

import '../controllers/cart_controller.dart';

class CartView extends GetView {
  @override
  final CartController controller = Get.put(CartController());

  CartView({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.white,
        title: const Text('购物车'),
        centerTitle: true,
        elevation: 0,
        actions: [
          Obx(() => controller.isEdit.value
              ? TextButton(
                  onPressed: () {
                    controller.changeEditState();
                  },
                  child: const Text("完成"))
              : TextButton(
                  onPressed: () {
                    controller.changeEditState();
                  },
                  child: const Text("编辑")))
        ],
      ),
      body: GetBuilder(
          init: controller,
          initState: (state) {
            controller.getCartListData();
          },
          builder: (controller) {
            return controller.cartList.isNotEmpty
                ? Stack(
                    children: [
                      ListView(
                        padding:
                            EdgeInsets.only(bottom: ScreenAdapter.height(200)),
                        children: controller.cartList
                            .map((value) => CartItemView(value))
                            .toList(),
                      ),
                      Positioned(
                          left: 0,
                          right: 0,
                          bottom: 0,
                          child: Container(
                            padding:
                                EdgeInsets.only(right: ScreenAdapter.width(20)),
                            width: ScreenAdapter.width(1080),
                            height: ScreenAdapter.height(190),
                            decoration: BoxDecoration(
                                color: Colors.white,
                                border: Border(
                                    top: BorderSide(
                                        color: const Color.fromARGB(
                                            178, 240, 236, 236),
                                        width: ScreenAdapter.height(2)))),
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                Row(
                                  children: [
                                    Checkbox(
                                        activeColor: Colors.red,
                                        value: controller.checkedAllBox.value,
                                        onChanged: (value) {
                                          controller.checkedAllFunc(value);
                                        }),
                                    const Text("全选")
                                  ],
                                ),
                                Obx(() => controller.isEdit.value
                                    ? Row(
                                        crossAxisAlignment:
                                            CrossAxisAlignment.center,
                                        children: [
                                          ElevatedButton(
                                              style: ButtonStyle(
                                                  backgroundColor:
                                                      MaterialStateProperty.all(
                                                          const Color.fromRGBO(
                                                              255, 165, 0, 0.9)),
                                                  foregroundColor:
                                                      MaterialStateProperty.all(
                                                          Colors.white),
                                                  shape: MaterialStateProperty.all(
                                                      RoundedRectangleBorder(
                                                          borderRadius:
                                                              BorderRadius
                                                                  .circular(10)))),
                                              onPressed: () {
                                                controller.deleteCartData();
                                              },
                                              child: const Text("删除"))
                                        ],
                                      )
                                    : Row(
                                        crossAxisAlignment:
                                            CrossAxisAlignment.center,
                                        children: [
                                          const Text("合计："),
                                         Obx(() =>  Text(
                                            "¥${controller.allPrice}",
                                            style: TextStyle(
                                                fontSize:
                                                    ScreenAdapter.fontSize(58),
                                                color: Colors.red),
                                          )),
                                          SizedBox(
                                              width: ScreenAdapter.width(20)),
                                          ElevatedButton(
                                              style: ButtonStyle(
                                                  backgroundColor:
                                                      MaterialStateProperty.all(
                                                          const Color.fromRGBO(
                                                              255, 165, 0, 0.9)),
                                                  foregroundColor:
                                                      MaterialStateProperty.all(
                                                          Colors.white),
                                                  shape: MaterialStateProperty.all(
                                                      RoundedRectangleBorder(
                                                          borderRadius:
                                                              BorderRadius
                                                                  .circular(10)))),
                                              onPressed: () {
                                                // Get.toNamed("/checkout");
                                                controller.checkout();
                                              },
                                              child: const Text("结算"))
                                        ],
                                      ))
                              ],
                            ),
                          ))
                    ],
                  )
                : const Center(
                    child: Text("购物车空空的"),
                  );
          }),
    );
  }
}
